//
//  JJBaseView.swift
//  JJSina
//
//  Created by mac on 2017/12/4.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit
import SnapKit

class JJBaseView: UIView {
    // 登录和注册按钮实现的闭包
    var clouser: (()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 外界设置视图的方法
    func setupBaseViewInfo(imageName: String?, title: String?) {
        if let tit = title, let imagN = imageName {
            iconImageView.image = UIImage(named: imagN)
            messageLabel.text = tit
            feedImageView.isHidden = true
        } else {
            setFeedImageViewAnim()
        }
    }
    // MARK: - 设置FeedImageView旋转动画
    private func setFeedImageViewAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = .pi * 2.0
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        feedImageView.layer.add(anim, forKey: nil)
    }
    // MARK: - 设置视图
    private func setupUI() {
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        // 添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        // 添加约束
        feedImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        iconImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        messageLabel.snp.makeConstraints { (make) in
        make.top.equalTo(maskImageView.snp.bottom).offset(16)
            make.width.equalTo(230)
        make.centerX.equalTo(iconImageView.snp.centerX)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(35)
        make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.left.equalTo(messageLabel.snp.left)
        }
        registerBtn.snp.makeConstraints { (make) in
       
            make.size.equalTo(CGSize(width: 100, height: 35))
        make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.right.equalTo(messageLabel.snp.right)
        }
        /*
        // 系统自带约束方法
    feedImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint.init(item: feedImageView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: feedImageView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
        */
        
    }
    
    // MARK: - 懒加载控件
    private lazy var feedImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮挡图片
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // icon
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        // 换行
        label.numberOfLines = 0
        label.textAlignment = .center
        // 字体大小
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
    // 登录按钮
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        // 监听事件
        btn.addTarget(self, action: #selector(loginOrRegisterClick), for: UIControlEvents.touchUpInside)
        // 背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white"), for: UIControlState.normal)
        // 文字
        btn.setTitle("登录", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(JJ_THEMECOLOR
            , for: UIControlState.highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        return btn
    }()
    // 注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        // 监听事件
        btn.addTarget(self, action: #selector(loginOrRegisterClick), for: UIControlEvents.touchUpInside)
        // 背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white"), for: UIControlState.normal)
        // 文字
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(JJ_THEMECOLOR
            , for: UIControlState.highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        return btn
    }()
    
    // MARK: -监听事件
    @objc private func loginOrRegisterClick() {
        clouser?()
    }
    
    
}
